package net.cloudcodex.client.character.presenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.cloudcodex.client.CloudCodexEventBus;
import net.cloudcodex.client.cdo.CharacterCDO;
import net.cloudcodex.client.character.view.CharacterView;
import net.cloudcodex.client.character.view.CharacterViewImpl;
import net.cloudcodex.client.character.view.CharactersView.Message;
import net.cloudcodex.client.character.view.panel.CharacterPanel;
import net.cloudcodex.client.storyboard.player.presenter.PlayerBoardPresenter;
import net.cloudcodex.client.util.Container;
import net.cloudcodex.client.util.rpc.SuccessCallback;
import net.cloudcodex.client.widgets.ToggleButtonGroup;
import net.cloudcodex.shared.dto.NotificationDTO;
import net.cloudcodex.shared.dto.Result;
import net.cloudcodex.shared.dto.campaign.character.CharacterDescriptionDTO;
import net.cloudcodex.shared.dto.campaign.character.CharacterHeaderDTO;
import net.cloudcodex.shared.dto.campaign.character.CompleteCharacterDescriptionDTO;
import net.cloudcodex.shared.dto.home.CharacterSummaryDTO;
import net.cloudcodex.shared.rpc.CampaignRPCAsync;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

/**
 * 
 * 
 * @author ADaroussin
 */
@Presenter(view = CharacterViewImpl.class, multiple = true)
public class CharacterPresenter extends
		BasePresenter<CharacterView, CloudCodexEventBus> {
	private final Message message;

	private final Map<String, IsWidget> listContents = new HashMap<String, IsWidget>();

	// protected CharacterPanel characterPanel = new CharacterPanel();
	private CampaignRPCAsync campaignService;
	private CharacterSummaryDTO characterSummaryDTO;
	protected String selectedContent;
	private boolean isInit = false;

	protected List<CharacterHeaderDTO> characterHeaderDTOList;

	private String nickName;

	private long userId;

	@Inject
	public CharacterPresenter(Message pMessage, CampaignRPCAsync campaignService) {
		this.message = pMessage;
		this.campaignService = campaignService;
	}

	@Override
	public void bind() {
		this.view.getNotificationListBox().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// ListBox listBox = (ListBox) event.getSource();
				// String text =
				// listBox.getItemText(listBox.getSelectedIndex());
				// System.out.println("test = " + text);

			}
		});

		this.view.getToggleButtonGroup().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				ToggleButtonGroup toggleButtonGroup = (ToggleButtonGroup) event
						.getSource();
				ToggleButton selectedButton = toggleButtonGroup
						.getSelectedButton();
				setSelectedContent(selectedButton.getTitle());
				view.setSelectedContent(selectedContent);
				refreshContent();

			}
		});
	}

	public void setSelectedContent(String selectedContent) {
		this.selectedContent = selectedContent;
	}

	protected void refreshContent() {

		this.view.setSelectedContent(selectedContent);
		view.refreshContent(listContents.get(selectedContent));
	}

	public void addCharacterSummary(CharacterSummaryDTO characterSummaryDTO) {
		this.characterSummaryDTO = characterSummaryDTO;

	}

	public void initView() {
		initNotificationPanel();
		initCharacterSheetView(characterSummaryDTO.getCampaign().getId(),
				characterSummaryDTO.getId());
		initStoryBoard();
		isInit = true;
	}

	public void display(){
		eventBus.addCharacterTab(view, characterSummaryDTO.getName(),
				characterSummaryDTO.getId());
		
	}
	
	private void initCharacterSheetView(final Long campaignID, final Long characterID) {

		campaignService.getCharacterCompleteDescription(campaignID,
				characterID,
				new SuccessCallback<Result<CompleteCharacterDescriptionDTO>>() {

					@Override
					public void onSuccess(
							Result<CompleteCharacterDescriptionDTO> result) {
						CompleteCharacterDescriptionDTO completeCharacterDescriptionDTO = result
								.getData();
						CharacterCDO characterCDO = new CharacterCDO(
								completeCharacterDescriptionDTO);
						CharacterDescriptionPresenter characterDescriptionPresenter = eventBus
								.addHandler(CharacterDescriptionPresenter.class);
						characterDescriptionPresenter.setByCharacterId(characterID);
						characterDescriptionPresenter
								.initCharacterDescriptionPanel(characterCDO);
						listContents.put(CharacterPanel.CHARACTER_SHEET,
								characterDescriptionPresenter.getView());
					}
				});

	}

	protected void initStoryBoard() {
		final Container<PlayerBoardPresenter> container = new Container<PlayerBoardPresenter>();
		eventBus.newPlayerBoard(characterSummaryDTO.getCampaign().getId(),
				characterSummaryDTO.getId(), container);
		listContents.put(CharacterPanel.STORYBOARD, container.get().getView());
	}

	/**
	 * 
	 */
	private void initNotificationPanel() {
		List<NotificationDTO> characterNotificationDTOs = characterSummaryDTO
				.getNotifications();
		if (characterNotificationDTOs != null
				&& !characterNotificationDTOs.isEmpty()) {
			for (NotificationDTO characterNotificationDTO : characterNotificationDTOs) {
				this.view.addNotification(
						characterNotificationDTO.getMessage(),
						characterNotificationDTO.getDate());

			}
		}
		List<NotificationDTO> campaignNotificationDTOs = characterSummaryDTO
				.getCampaign().getNotifications();
		if (campaignNotificationDTOs != null
				&& !campaignNotificationDTOs.isEmpty()) {
			for (NotificationDTO campaignNotificationDTO : campaignNotificationDTOs) {

				this.view.addNotification(campaignNotificationDTO.getMessage(),
						campaignNotificationDTO.getDate());
			}
		}
	}

//	private void initCharactersList() {
//		campaignService.getCampaignCharacters(characterSummaryDTO.getCampaign()
//				.getId(),
//				new SuccessCallback<Result<List<CharacterHeaderDTO>>>() {
//					@Override
//					public void onSuccess(
//							Result<List<CharacterHeaderDTO>> result) {
//						final List<CharacterHeaderDTO> characterHeaderDTOs = result
//								.getData();
//						characterHeaderDTOList = characterHeaderDTOs;
//					}
//
//				});
//	}


	public void onSetNickName(String nickName) {
		this.nickName = nickName;
	}

	public boolean isViewInitialized(){
		return isInit ;
	}

}
